from django.contrib import admin

# Register your models here.
from .models import HostsInfo
from utils.decorators import log_oper_decorator


@log_oper_decorator
class HostsInfoAdmin(admin.ModelAdmin):
    """
    celery task
    任务基本信息管理
    """
    model = HostsInfo
    list_display = ('host_flag', 'ip', 'port', 'username', 'colored_is_deleted', 'c_time')
    list_filter = ('ip', 'username')
    readonly_fields = ('c_time', 'm_time')
    search_fields = ('host_flag', 'ip', 'username')
    fieldsets = (
        ("主机基本信息", {
            'fields': (
                'host_flag',
                'ip',
                'port',
                'username',
                'password',
            ),
            'classes': ('extrapretty', 'wide')
        }),
        ("其他信息", {
            'fields': (
                'is_deleted',
                'c_time',
                'm_time',
            ),
            'classes': ('extrapretty', 'wide')
        }),
    )

    def delete_queryset(self, request, queryset):
        # queryset.delete()
        queryset.update(is_deleted=1)

    def delete_model(self, request, obj):
        """
        Given a model instance delete it from the database.
        """
        # obj.delete()
        obj.is_deleted = 1
        obj.save()


admin.site.register(HostsInfo, HostsInfoAdmin)
